<template>

  <div class="tabs" v-if="showTabs">
    <!-- 标签页 -->
    <ul>
      <li class="tab" v-for="(item,index) in tabs" :class="{'active':isActive(item.path)}" :key="index">
        <router-link :to="item.path" class="tab-title">
          {{item.title}}
        </router-link>
        <span class="tab-icon" @click="tabClose(index)">
          <i class="el-icon-close"></i>
        </span>
      </li>
    </ul>
    <!-- 标签操作 -->
    <div class="tabs-close-box">
      <el-dropdown @command="tabsHandle">
        <el-button size="mini" type="primary">
          标签选项<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu size="small" slot="dropdown">
          <el-dropdown-item command="other">关闭其他</el-dropdown-item>
          <el-dropdown-item command="all">关闭所有</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div>
    </div>
  </div>


</template>

<script>
  export default {
    data() {
      return {
        tabs: []
      }
    },
    methods: {
      /**
       * 判断路径是否为当前路由路径
       * @param path
       * @returns {boolean}
       */
      isActive(path) {
        return path === this.$route.fullPath;
      },
      /**
       * 关闭单个标签
       * @param index 标签索引
       */
      tabClose(index) {
        // 找到需要关闭的标签
        let target = this.tabs.splice(index, 1)[0];
        // 找到关闭后，下一个需要显示的标签
        let toShow = this.tabs[index] ? this.tabs[index] : this.tabs[index - 1];
        if (toShow) {
          target.path === this.$route.fullPath && this.$router.push(toShow.path);
        } else {
          this.$router.push('/');
        }
      },

      tabsHandle(command) {
        (command === 'other') ? this.tabOtherClose() : this.tabAllClose();
      },
      /**
       * 关闭所有标签
       */
      tabAllClose() {
        this.tabs = [];
        this.$router.push('/');
      },
      /**
       * 关闭其他标签
       */
      tabOtherClose() {
        this.tabs = this.tabs.filter(item => item.path === this.$route.fullPath);
      },
      /**
       * 添加标签
       */
      tabAdd(route) {
        // 判断路由是否在标签中已存在
        let existed = this.tabs.some(item => item.path === route.fullPath);
        if (!existed) {
          // todo 最多可以显示多少个标签页
          // 添加到标签列表
          this.tabs.push({
            title: route.meta.title,
            path: route.fullPath
          })
        }
      }
    },
    computed: {
      showTabs() {
        return this.tabs.length > 0;
      }
    },
    watch: {
      /**
       * 观察路由变化
       * @param newValue
       * @param oldValue
       */
      $route(newValue, oldValue) {
        this.tabAdd(newValue);
      }
    },
    created() {
      this.tabAdd(this.$route);
    }
  }
</script>

<style>
  .tabs {
    position: relative;
    height: 30px;
    overflow: hidden;
    background: #fff;
    padding-right: 120px;
    box-shadow: 0 5px 10px #ddd;
  }

  .tabs ul {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
  }

  .tab {
    float: left;
    margin: 3px 5px 2px 3px;
    border-radius: 3px;
    font-size: 12px;
    overflow: hidden;
    cursor: pointer;
    height: 23px;
    line-height: 23px;
    border: 1px solid #e9eaec;
    background: #fff;
    padding: 0 5px 0 12px;
    vertical-align: middle;
    color: #666;
    -webkit-transition: all .3s ease-in;
    -moz-transition: all .3s ease-in;
    transition: all .3s ease-in;
  }

  .tab:not(.active):hover {
    background: #f8f8f8;
  }

  .tab-title {
    float: left;
    max-width: 80px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-right: 5px;
    color: #666;
  }

  .tab.active {
    border: 1px solid #409EFF;
    background-color: #409EFF;
  }

  .tab.active .tab-title {
    color: #fff;
  }

  .tabs-close-box {
    position: absolute;
    right: 0;
    top: 0;
    box-sizing: border-box;
    padding-top: 1px;
    text-align: center;
    width: 110px;
    height: 30px;
    background: #fff;
    box-shadow: -3px 0 15px 3px rgba(0, 0, 0, .1);
    z-index: 10;
  }

</style>
